package test2zero;

import java.awt.BorderLayout;  
import java.awt.Dimension;  
import java.awt.event.ActionEvent;  
import java.awt.event.ActionListener;  
import java.net.URL;  
  
//import javafx.collections.Sequence;  
import javafx.embed.swing.JFXPanel;  
 import javafx.scene.Group;  
import javafx.scene.Node;  
import javafx.scene.Scene;  
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.web.WebEngine;  
import javafx.scene.web.WebView;  
import javafx.stage.Stage;  
  
import javax.swing.JButton;  
 import javax.swing.JFrame;  
import javax.swing.JPanel;  
import javax.swing.SwingUtilities;  
  
import com.sun.javafx.application.PlatformImpl;  
  
public class SwingFXWebView extends JPanel {  
    
    private Stage stage;  
    private WebView browser;  
    private JFXPanel jfxPanel;  
    private JButton swingButton;  
    private WebEngine webEngine;  
  
    public SwingFXWebView(){  
        initComponents();  
    }  
  
    public static void main(String ...args){  
     
        SwingUtilities.invokeLater(new Runnable() {  
            public void run() {  
                final JFrame frame = new JFrame();  
                 
                frame.getContentPane().add(new SwingFXWebView());  
                 
                frame.setMinimumSize(new Dimension(640, 480));  
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                frame.setVisible(true);  
            }  
        });     
    }  
     
    private void initComponents(){  
         
        jfxPanel = new JFXPanel();  
        createScene();  
         
        setLayout(new BorderLayout());  
        add(jfxPanel, BorderLayout.CENTER);  
         
        swingButton = new JButton();  
        swingButton.addActionListener(new ActionListener() {  
             
            @Override  
            public void actionPerformed(ActionEvent e) {  
                webEngine.reload();  
            }  
        });  
        swingButton.setText("Reload");  
         
        add(swingButton, BorderLayout.SOUTH);  
    }     
     
    /** 
     * createScene 
     * 
     * Note: Key is that Scene needs to be created and run on "FX user thread" 
     *       NOT on the AWT-EventQueue Thread 
     * 
     */  
    private void createScene() {  
        PlatformImpl.startup(new Runnable() {  
            public void run() {  
                 
                stage = new Stage();  
                 
                stage.setTitle("Hello Java FX");  
                stage.setResizable(false);  
   
                Group root = new Group();  
                for (int i = 0; i < 5; i++) {
                    Rectangle r = new Rectangle();
                    r.setY(i * 20);
                    r.setWidth(100);
                    r.setHeight(10);
                    r.setFill(Color.RED);
                    root.getChildren().add(r);
                }
                Scene scene = new Scene(root,80,20);  
                stage.setScene(scene);  
                 
                 
                URL url = null;  
               /* try{  
                    url = new URL("<a href="http://www.google.ie" target="_blank">http://www.google.ie</a>");  
                }catch(Exception ex){  
                    ex.printStackTrace();  
                }  */
                /*webEngine = new WebEngine(url);  
                browser = new WebView(webEngine);  
  
                Sequence<Node> children = root.getChildren();  
                children.add(browser);                     
                 */
                jfxPanel.setScene(scene);  
            }  
        });  
    }     
}  